﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Subjects;
using System.Text;
using Frogs.Stability.CircuitBreaker.Exceptions;

namespace Frogs.Stability.CircuitBreaker.Behaviors.Functions
{
    internal delegate void HalfOpenCircuitSuccessEventHandler(object sender, EventArgs args);

    class HalfOpenBehavior : IFunctionCircuitBehavior
    {
        public T DoFunction<T>(Func<T> func)
        {
            try
            {
                T value = func();
				this._executionResult.OnNext(Behaviors.ExecutionResult.Success);
                return value;
            }
            catch (Exception excp)
            {
				this._executionResult.OnNext(Behaviors.ExecutionResult.Fail);
                throw new HalfOpenCircuitBreakerException("Half-open circuit breaker failed, see inner exception for details", excp);
            }
        }

        public CircuitState GetStatus()
        {
            return CircuitState.HalfWay;
        }
	
		Subject<ExecutionResult> _executionResult = new Subject<ExecutionResult>();
		public IObservable<ExecutionResult> ExecutionResult { get { return this._executionResult; } }
	}
}
